﻿using System.Windows.Controls;
namespace C4F.VistaP2P.WPF
{
    public class Utility
    {
        delegate string GetTextboxTextHandler(TextBox t);
        public static string GetTextboxText(TextBox t)
        {
            if (t == null)
            {
                return null;
            }
            if (t.CheckAccess())
            {
                // The calling thread owns the dispatcher, and hence the UI element
                return t.Text;
            }
            else
            {
                // Invocation required
                return (string)t.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new GetTextboxTextHandler(GetTextboxText), t);
            }
        }

        delegate void SetButtonTextHandler(Button t, string content);
        public static void SetButtonText(Button b, string text)
        {
            if (b != null)
            {
                if (b.CheckAccess())
                {
                    // The calling thread owns the dispatcher, and hence the UI element
                    b.Content = text;
                }
                else
                {
                    // Invocation required
                    b.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new SetButtonTextHandler(SetButtonText), b, text);
                }
            }
        }

        delegate void SetTextboxTextHandler(TextBox t, string content);
        internal static void SetTextBoxText(TextBox tb, string text)
        {
            if (tb != null)
            {
                if (tb.CheckAccess())
                {
                    // The calling thread owns the dispatcher, and hence the UI element
                    tb.Text = text;
                }
                else
                {
                    // Invocation required
                    tb.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new SetTextboxTextHandler(SetTextBoxText), tb, text);
                }
            }
        }

        delegate void SetLabelTextHandler(Label l, string content);
        internal static void SetLabelText(Label l, string p)
        {
            if (l != null)
            {
                if (l.CheckAccess())
                {
                    // The calling thread owns the dispatcher, and hence the UI element
                    l.Content = p;
                }
                else
                {
                    // Invocation required
                    l.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new SetLabelTextHandler(SetLabelText), l, p);
                }
            }
        }

        delegate void SetContentControlStateHandler(Control c, bool isEnabled);
        internal static void SetControlEnabledState(Control c, bool isEnabled)
        {
            if (c != null)
            {
                if (c.CheckAccess())
                {
                    // The calling thread owns the dispatcher, and hence the UI element
                    c.IsEnabled = isEnabled;
                }
                else
                {
                    // Invocation required
                    c.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new SetContentControlStateHandler(SetControlEnabledState), c, isEnabled);
                }
            }
        }
    }
}
